/**
 * Created by lesonli on 2016/11/17.
 */
import * as types from '../types'
import api from '../../api/modules/config'
import {
    createUser,
    deleteUser,
    updateUser,
    getUserList
} from '../../api/modules/user'

const state = {
    user_list: [],
    showLoading: false,
    total:100
}

// getters
// actions
const actions = {
        get_user_list({
            commit
        }, param) {
            commit(types.USER_SHOW_LOADING, param);
            getUserList(param).then((res) => {
                    if (res.data.res_code === 1) {
                        commit(types.USER_LIST_LOADED, res.data.msg);
                    }
                });
        },
        add_user({
            commit
        }, param) {
            commit(types.USER_SHOW_LOADING, param);
            createUser(param).then((res) => {
                    if (res.data.res_code === 1) {
                        commit(types.USER_ADDED, {result:res.data.msg,data:param});
                        if(param._fn)
                            param._fn();
                    }
                });
        },
        update_user({
            commit
        }, param) {
            commit(types.USER_SHOW_LOADING, param);
            updateUser(param).then((res) => {
                    if (res.data.res_code === 1) {
                        commit(types.USER_UPDATED, param);
                        if(param._fn)
                            param._fn();
                    }
                });
        },
        delete_user({
            commit
        }, param) {
            commit(types.USER_SHOW_LOADING, param);
            deleteUser(param.user_id).then((res) => {
                    if (res.data.res_code === 1) {
                        commit(types.USER_DELETED, param);
                        if(param._fn)
                            param._fn();
                    }
                });
        }
    }
    // mutations
const mutations = {
    [types.USER_SHOW_LOADING](state, param) {
        state.showLoading = true;
    },
    [types.USER_LIST_LOADED](state, param) {
        // state.user_list = param.list;
        state.user_list = param;
        state.total = param.total;
        state.showLoading = false;
    },
    [types.USER_ADDED](state, param) {
        state.user_list.unshift({user_id:param.result,realname:param.data.realname,phone:param.data.phone,email:param.data.email,role_id:param.data.role_id,dealer_id:param.data.dealer_id});
        state.showLoading = false;
    },
    [types.USER_UPDATED](state, param) {
        for(var i=0;i<state.user_list.length;i++)
        {
            if(state.user_list[i].user_id === param.user_id)
            {
                state.user_list[i].pwd = param.pwd;
                state.user_list[i].realname = param.realname;
                state.user_list[i].phone = param.phone;
                state.user_list[i].email = param.email;
                state.user_list[i].role_id = param.role_id;
                state.user_list[i].dealer_id = param.dealer_id;
            }
        }
        state.showLoading = false;
    },
    [types.USER_DELETED](state, param) {
        for(var i=0;i<state.user_list.length;i++)
        {
            if(state.user_list[i].user_id === param.user_id)
            {
                state.user_list.splice(i,1);
                break;
            }
        }
        state.showLoading = false;
    }
}

export default {
    state,
    actions,
    mutations
}
